﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.UI.Report.Views.Interfaces;
using Common.UI.Report.Views;
using Common.Model;
using Common.ServiceLogic;

namespace Common.UI.Report
{
    public class GenericReportController<V, C, S> : Common.UI.EM.GenericController
        where V : ViewEntity
        where C : CriteriaEntity, new()
        where S : IGenericReportServiceLogic<V, C>, new()
    {
        public virtual String Title
        {
            get { return string.Empty; }
        }

        public IReportPresentationModel<V, C, S> ReportPresentationModel
        {
            get;
            set;
        }

        public IReportView ReportView
        {
            get;
            set;
        }

        public override void Run()
        {
            this.CreatePresentationModels();

            this.CreateReportView();

            this.RegisterEventHandlers();

            this.InitializePresentationModels();

            this.RegisterReportView();
        }

        private void CreatePresentationModels()
        {
            this.CreateEntityPresentationModel();
        }

        protected virtual void CreateEntityPresentationModel()
        {
            this.ReportPresentationModel = new ReportPresentationModel<V, C, S>(this.CreateFilterView(), this.CreateListView());
        }

        protected IFilterView CreateFilterView()
        {
            var dv = new FilterView();
            dv.ContentControl = this.CreateFilterDataView();
            return dv;
        }

        protected virtual FilterDataView CreateFilterDataView()
        {
            return new FilterDataView();
        }


        protected ListView CreateListView()
        {
            return new ListView();
        }

        private void CreateReportView()
        {
            ReportView = new ReportView(this.ReportPresentationModel.FilterView, this.ReportPresentationModel.ListView);
            ReportView.Title = this.Title;
        }

        private void RegisterEventHandlers()
        {
            this.ReportPresentationModel.SearchCalled += new SearchCalledEventHandler(FilterPresentationModel_SearchCalled);
            this.RegisterCustomEventHandlers();
        }
        
        protected virtual void RegisterCustomEventHandlers()
        {
        }

        
        void FilterPresentationModel_SearchCalled(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<CriteriaEntity> e)
        {
            ReportView.ShowFilter();
        }

        private void InitializePresentationModels()
        {
            //FILTER
            this.ReportPresentationModel.LoadCriteria(new C());
            this.AddColumnDefinitions();

            //LIST
            this.ReportPresentationModel.AllowExport = Common.Security.Authorization.CanExecuteExport(this.GetFullName());
            this.ReportPresentationModel.AllowPrint = Common.Security.Authorization.CanExecutePrint(this.GetFullName());
        }

        protected virtual void AddColumnDefinitions()
        {
        }


        private void RegisterReportView()
        {
            MainView.View.RegisterView(this.Title, this.ReportView);
        }

    }
}
